import { defineStore } from 'pinia'
import { TYPE_SCHEMA, QUESTION_TYPE_DESCRIPTION, QUESTION_TYPE_SINGLE, QUESTION_TYPE_MULTI, QUESTION_TYPE_SELECT, QUESTION_TYPE_MATRIX_RADIO } from '@/constants/default'

export default defineStore('newSurveyData', {
  state: () => ({
    title: '',
    info: '',
    questions: [],
    currentOrder: -1,
    submitting: false,
    created: false,
    surveyUrl: '',
    addFromCurrent: false,
    batchEdit: false,
    isSubTitle: false, // 和 batchEdit 搭配使用
  }),
  actions: {
    addQuestion(questionType) {
      const newQuestion = {
        ...JSON.parse(JSON.stringify(TYPE_SCHEMA[questionType])),
        id: Math.random(),
      }
      if (this.addFromCurrent && this.currentOrder !== -1) {
        const qs = JSON.parse(JSON.stringify(this.questions))
        qs.splice(this.currentOrder, 0, newQuestion),
          qs.forEach((v, i) => {
            v.order = i
          })
        this.questions = qs
        this.currentOrder += 1
      } else {
        this.questions.push({
          ...newQuestion,
          order: this.questions.length,
        })
      }

      this.storageQuestions()
    },
    delQuestion() {
      const qs = JSON.parse(JSON.stringify(this.questions))
      const co = this.currentOrder
      qs.splice(co, 1)
      qs.forEach((q, i) => {
        q.order = i
      })
      this.questions = qs
      this.currentOrder = -1
      this.storageQuestions()
    },
    clearData() {
      this.title = ''
      this.info = ''
      this.questions = []
      this.currentOrder = -1
      window.localStorage.removeItem('survey_questions')
      window.localStorage.removeItem('survey_title')
      window.localStorage.removeItem('survey_info')
    },
    loadFromLocal() {
      const title = window.localStorage.getItem('survey_title') || ''
      const info = window.localStorage.getItem('survey_info') || ''
      const questions = JSON.parse(window.localStorage.getItem('survey_questions')) || []
      this.title = title
      this.info = info
      this.questions = questions
    },
    importData(surveyTemplate) {
      const title  = surveyTemplate.survey_title || ''
      const info  = surveyTemplate.survey_info || ''
      const questions  = surveyTemplate.survey_questions || []
      this.title = title
      this.info = info
      this.questions = questions
      window.localStorage.setItem('survey_title', title )
      window.localStorage.setItem('survey_info', info )
      window.localStorage.setItem('survey_questions', JSON.stringify(questions ))
    },

    modifyTitle(t) {
      this.title = t
      window.localStorage.setItem('survey_title', t)
    },
    modifyInfo(i) {
      this.info = i
      window.localStorage.setItem('survey_info', i)
    },
    modifyCurrentOrder(order) {
      this.currentOrder = order
      const type = this.questions[order].type
      if (!(type === QUESTION_TYPE_SINGLE || type === QUESTION_TYPE_MULTI || type === QUESTION_TYPE_SELECT || type === QUESTION_TYPE_MATRIX_RADIO)) {
        this.modifyBatchEdit(false)
        return
      }
    },

    storageQuestions() {
      window.localStorage.setItem('survey_questions', JSON.stringify(this.questions))
    },
    modifyQuestionTitle(order, t) {
      this.questions[order].title = t
      this.storageQuestions()
    },
    modifyQuestionInfo(order, i) {
      this.questions[order].info = i
      this.storageQuestions()
    },

    toggleRequired() {
      const co = this.currentOrder
      const qs = this.questions

      if (co < 0) return
      if (qs[co].type === QUESTION_TYPE_DESCRIPTION) return
      this.questions[co].required = !qs[co].required
      this.storageQuestions()
    },
    toggleAddFromCurrent() {
      this.addFromCurrent = !this.addFromCurrent
    },
    modifyBatchEdit(flag, isSubTitle) {
      this.batchEdit = flag
      this.isSubTitle = !!isSubTitle
    },

    moveUp() {
      const co = this.currentOrder
      const qs = JSON.parse(JSON.stringify(this.questions))
      const t = qs[co]
      if (co < 1) return
      qs[co].order = co - 1
      qs[co - 1].order = co
      qs[co] = qs[co - 1]
      qs[co - 1] = t
      this.currentOrder = co - 1
      this.questions = qs
      this.storageQuestions()
    },
    moveDown() {
      const co = this.currentOrder
      const qs = JSON.parse(JSON.stringify(this.questions))
      const t = qs[co]
      if (co >= qs.length - 1) return
      qs[co].order = co + 1
      qs[co + 1].order = co
      qs[co] = qs[co + 1]
      qs[co + 1] = t
      this.currentOrder = co + 1
      this.questions = qs
      this.storageQuestions()
    },
    resetCurrentOrder() {
      this.currentOrder = -1
    },
    // NOTE: 单选、多选、下拉、矩阵单选的增加，是直接调用。
    delOption(order, optionIndex) {
      this.questions[order].options.splice(optionIndex, 1)
      this.storageQuestions()
    },
    delSubTitle(order, optionIndex) {
      this.questions[order].subTitles.splice(optionIndex, 1)
      this.storageQuestions()
    },
  },
})
